<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\member\controller;

class AdminMember extends \app\AdminRest
{
    protected $model;
    protected $rights_model;
    protected $coach_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\member\model\Level();
        $this->rights_model = new \app\member\model\Rights();
        $this->coach_model = new \app\massage\model\Coach();
        $this->rights_model->initData($this->_uniacid);
    }
    public function levelList()
    {
        $input = $this->_param;
        $dis[] = ["status", ">", -1];
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $data = $this->model->dataList($dis, $input["limit"]);
        $user_model = new \app\massage\model\User();
        if(!empty($data["data"])) {
            foreach ($data["data"] as $k => &$v) {
                $v["vip"] = ($data["current_page"] - 1) * $input["limit"] + $k + 1;
                $next = $this->model->where(["status" => 1, "uniacid" => $this->_uniacid])->where("growth", ">", $v["growth"])->order("growth,id desc")->find();
                if(!empty($next)) {
                    $next->growth = $next->growth - 0;
                    $v["member_num"] = $user_model->where(["uniacid" => $this->_uniacid])->where("growth", "between", $v["growth"] . "," . $next->growth)->count();
                } else {
                    $v["member_num"] = $user_model->where(["uniacid" => $this->_uniacid])->where("growth", ">=", $v["growth"])->count();
                }
            }
        }
        return $this->success($data);
    }
    public function levelAdd()
    {
        $input = $this->_input;
        $find = $this->model->where(["growth" => $input["growth"], "uniacid" => $this->_uniacid])->where("status", ">", -1)->find();
        if(!empty($find)) {
            $this->errorMsg("不能添加相同成长值的等级");
        }
        $min = $this->model->where(["uniacid" => $this->_uniacid])->where("growth", "<", $input["growth"])->where("status", "=", 0)->find();
        if(!empty($min)) {
            $this->errorMsg("请先开启下级等级");
        }
        $insert = ["uniacid" => $this->_uniacid, "title" => $input["title"], "color" => $input["color"], "growth" => $input["growth"]];
        $this->model->dataAdd($insert);
        $id = $this->model->getLastInsID();
        $connect_model = new \app\member\model\RightsConnect();
        $coupon_model = new \app\member\model\Coupon();
        if(!empty($input["rights"])) {
            foreach ($input["rights"] as $k => $v) {
                $inserts[$k] = ["uniacid" => $this->_uniacid, "level_id" => $id, "rights_id" => $v];
            }
            $connect_model->saveAll($inserts);
        }
        if(!empty($input["coupon"])) {
            foreach ($input["coupon"] as $k => $v) {
                $insert_data[$k] = ["uniacid" => $this->_uniacid, "level_id" => $id, "coupon_id" => $v["coupon_id"], "num" => $v["num"]];
            }
            $coupon_model->saveAll($insert_data);
        }
        return $this->success(true, 200, $id);
    }
    public function levelInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->model->dataInfo($dis);
        $data["rights"] = array_values(array_column($data["rights"], "rights_id"));
        $data["next_level"] = $this->model->where("status", ">", -1)->where("growth", ">", $data["growth"])->order("growth,id desc")->find();
        return $this->success($data);
    }
    public function levelUpdate()
    {
        $input = $this->_input;
        $find = $this->model->where(["growth" => $input["growth"], "uniacid" => $this->_uniacid])->where("id", "<>", $input["id"])->where("status", ">", -1)->find();
        if(!empty($find)) {
            $this->errorMsg("不能添加相同成长值的等级");
        }
        $dis = ["id" => $input["id"]];
        $insert = ["uniacid" => $this->_uniacid, "title" => $input["title"], "color" => $input["color"], "growth" => $input["growth"]];
        $this->model->dataUpdate($dis, $insert);
        $id = $input["id"];
        $connect_model = new \app\member\model\RightsConnect();
        $coupon_model = new \app\member\model\Coupon();
        $connect_model->where(["level_id" => $id])->delete();
        $coupon_model->where(["level_id" => $id])->delete();
        if(!empty($input["rights"])) {
            foreach ($input["rights"] as $k => $v) {
                $inserts[$k] = ["uniacid" => $this->_uniacid, "level_id" => $id, "rights_id" => $v];
            }
            $connect_model->saveAll($inserts);
        }
        if(!empty($input["coupon"])) {
            foreach ($input["coupon"] as $k => $v) {
                $insert_data[$k] = ["uniacid" => $this->_uniacid, "level_id" => $id, "coupon_id" => $v["coupon_id"], "num" => $v["num"]];
            }
            $coupon_model->saveAll($insert_data);
        }
        return $this->success(true);
    }
    public function levelStatusUpdate()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $data = $this->model->dataInfo($dis);
        if($input["status"] == -1 && $data["status"] != 0) {
            $this->errorMsg("请先停用");
        }
        if($input["status"] == 0) {
            $max = $this->model->where(["uniacid" => $this->_uniacid, "status" => 1])->order("growth desc,id desc")->find();
            if($input["id"] != $max->id) {
                $this->errorMsg("请先停用最高等级");
            }
        }
        if($input["status"] == 1) {
            $min = $this->model->where(["uniacid" => $this->_uniacid, "status" => 0])->order("growth,id desc")->find();
            if($input["id"] != $min->id) {
                $this->errorMsg("请先启用开启等级的最近等级");
            }
        }
        $res = $this->model->dataUpdate($dis, ["status" => $input["status"]]);
        return $this->success($res);
    }
    public function rightsList()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", ">", -1];
        $data = $this->rights_model->dataList($dis, $input["limit"]);
        return $this->success($data);
    }
    public function rightsSelect()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        $data = $this->rights_model->where($dis)->field("title,id,key,show_title")->order("top desc,id desc")->select()->toArray();
        return $this->success($data);
    }
    public function rightsInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->rights_model->dataInfo($dis);
        return $this->success($data);
    }
    public function rightsUpdate()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->rights_model->dataUpdate($dis, $input);
        return $this->success($data);
    }
    public function configInfo()
    {
        $input = $this->_param;
        $config_model = new \app\member\model\Config();
        $dis = ["uniacid" => $this->_uniacid];
        $data = $config_model->dataInfo($dis);
        return $this->success($data);
    }
    public function configUpdate()
    {
        $input = $this->_input;
        $config_model = new \app\member\model\Config();
        $dis = ["uniacid" => $this->_uniacid];
        $data = $config_model->dataUpdate($dis, $input);
        return $this->success($data);
    }
    public function levelSelect()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.status", "=", 1];
        if(!empty($input["is_service"])) {
            $dis[] = ["c.key", "=", "appoint_service"];
        }
        $data = $this->model->alias("a")->join("massage_member_rights_connect b", "a.id = b.level_id", "left")->join("massage_member_rights c", "b.rights_id = c.id", "left")->where($dis)->field("a.*")->group("a.id")->order("growth,id desc")->select()->toArray();
        return $this->success($data);
    }
}

?>